//
//  SicknessViewController.m
//  QuickCure1
//
//  Created by Haozhong Chen on 2016/11/2.
//  Copyright © 2016年 maoge. All rights reserved.
//

#import "SicknessViewController.h"
#import "NetWorkTool.h"
#import "NetWorkManager.h"
#import "Disease.h"
#import "SicknessPickViewController.h"
#import "SicknessComplicationData.h"
#import "YYModel.h"
#import "MatchedDoctorController.h"

@interface SicknessViewController ()<passPickedDataDelegate>

@property(nonatomic,strong)NSArray<Disease*>*dataList;
@property(nonatomic,strong)NSArray<SicknessComplicationData*>*complicationDataList;
@property(nonatomic,strong)Disease*pickedDisease;
@property(nonatomic,assign)BOOL isPicked;
@property(nonatomic,assign)BOOL isClickedComplication;
@property(nonatomic,assign)BOOL isConservativeTreat;

@property (weak, nonatomic) IBOutlet UILabel *label_c1;

@property (weak, nonatomic) IBOutlet UILabel *label_c2;

@property (weak, nonatomic) IBOutlet UILabel *label_c3;

@property (weak, nonatomic) IBOutlet UILabel *label_c4;

@property (weak, nonatomic) IBOutlet UIButton *button_a1;
@property (weak, nonatomic) IBOutlet UIButton *button_a2;

@property (weak, nonatomic) IBOutlet UIButton *button_b1;
@property (weak, nonatomic) IBOutlet UIButton *button_b2;
@property (weak, nonatomic) IBOutlet UIView *view_c2;
@property (weak, nonatomic) IBOutlet UIView *view_c3;


@property (weak, nonatomic) IBOutlet UIView *view_c4;
@property (weak, nonatomic) IBOutlet UILabel *label_matchedDoctorCount;
@property (weak, nonatomic) IBOutlet UIButton *button_apply;


/*
 @"diagnosis_type":@0,
 @"page_size":@15,
 @"is_confirmed":@1,
 @"user_id":@1000089,
 @"page":@1,
 @"has_diagnosis":@2
 */

@property(nonatomic,strong)NSNumber*diagnosis_type;
@property(nonatomic,strong)NSNumber*is_confirmed;
@property(nonatomic,strong)NSNumber*has_diagnosis;


@end

@implementation SicknessViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    
    self.view.backgroundColor=[UIColor lightGrayColor];
    [self loadData];
    
    [self setLabel_C1WithCi1_id:(self.ci1_id)];
    [self setupUI];
    self.isPicked=NO;
    self.isClickedComplication=NO;
    self.isConservativeTreat=YES;
    // Do any additional setup after loading the view.
}




#pragma mark - 点击申请就诊按钮
- (IBAction)clickedApplyButton:(id)sender
{
    //应该根据以下数据获取匹配医生的列表
    /*
     
     @{@"ci1_id":@1,
     @"ci2_id":@3,
     @"ci3_id":@3,
     @"diagnosis_type":@0,
     @"page_size":@15,
     @"is_confirmed":@1,
     @"user_id":@1000089,
     @"page":@1,
     @"has_diagnosis":@2
     };
     
     */
    
    //数据都在属性中
    //但接口的参数是死的,所以没什么卵用
    //所以直接跳转到一个固定的医生列表
    //数据由跳转后的控制器获取
    
    
    MatchedDoctorController*vc=[[MatchedDoctorController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
    
}


#pragma mark - 设置表头的label

-(void)setLabel_C1WithCi1_id:(NSInteger)ci1_id
{
    
    NSString*string;
    switch (ci1_id)
    {
        case 1:
            string=@"肿瘤疾病";
            break;
        case 2:
            string=@"心血管疾病";
            break;
        case 3:
            string=@"神经科疾病";
            break;
        case 4:
            string=@"血液科疾病";
            break;
        case 5:
            string=@"骨科疾病";
            break;
        default:
            string=@"";
            break;
    }
    
    self.label_c1.text= [@"疾病类型:" stringByAppendingString:string];
}



#pragma mark - 选择二级疾病
-(void)pickedDiease:(Disease*)diease
{
    
    self.pickedDisease=diease;
    self.label_c2.text=diease.ci3_name;
    self.isPicked=NO;
}
#pragma mark - 选择并发症
-(void)pickedComplicationList:(NSArray<SicknessComplicationData*>*)complicationList
{
    
    NSInteger count=complicationList.count;
    if (count==2)
    {
        
        
         NSString*string=[@"[" stringByAppendingString:complicationList[count-1].complication_name];
        NSString*secondString=[[@"," stringByAppendingString:complicationList[count-2].complication_name] stringByAppendingString:@"][共两条]"];
        self.label_c3.text=[string stringByAppendingString:secondString];
        
    }
    else if(count==1)
    {
        self.label_c3.text=complicationList[0].complication_name;
        
    }
    else
        self.label_c3.text=@"";
    
    
    
    self.isClickedComplication=NO;
}
#pragma mark - 选择治疗方案
-(void)pickedTreatment:(BOOL)isConservativeTreat
{
    if (isConservativeTreat)
    {
        self.label_c4.text=@"保守治疗";
        self.diagnosis_type=[[NSNumber alloc]initWithInt:0];
        
    }
    else
    {
        self.label_c4.text=@"先花他一个亿";
        self.diagnosis_type=[[NSNumber alloc]initWithInt:1];
    }
    
    [self loadMatchedDoctor];
    self.button_apply.enabled=YES;
    
    [self.button_apply setBackgroundColor:kThemeColor];
}




#pragma mark - 加载数据
-(void)loadData
{
    
    [self loadDataWithCi1_id:self.ci1_id];
    //    [self loadMatchedDoctor];
    
    [self loadComplicationData];
    
    //        [self test4];
}

#pragma mark - 添加手势识别

-(void)setupUI
{
    
    UITapGestureRecognizer *tapC2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(view_C2Clicked:)];
    [self.view_c2 addGestureRecognizer:tapC2];
    
    UITapGestureRecognizer *tapC3 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(view_C3Clicked:)];
    [self.view_c3 addGestureRecognizer:tapC3];
    
    
    UITapGestureRecognizer *tapC4 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(view_C4Clicked:)];
    [self.view_c4 addGestureRecognizer:tapC4];
    
    self.button_apply.enabled=NO;
}

#pragma mark - 点击跳转选择界面
-(void)view_C2Clicked:(UITapGestureRecognizer *)tap
{
    SicknessPickViewController*vc=[[SicknessPickViewController alloc]init];
    //    vc.view.backgroundColor=[UIColor greenColor];
    vc.dataList=self.dataList;
    vc.delegate=self;
    vc.isClickedDiease=YES;
    self.isPicked=YES;
    //    [self.navigationController presentViewController:vc animated:YES completion:nil];
    [self.navigationController pushViewController:vc animated:YES];

}
-(void)view_C3Clicked:(UITapGestureRecognizer *)tap
{
    SicknessPickViewController*vc=[[SicknessPickViewController alloc]init];
    //    vc.view.backgroundColor=[UIColor greenColor];
    vc.complicationDataList=self.complicationDataList;
    vc.delegate=self;
    vc.isClickedComplication=YES;
    self.isClickedComplication=YES;
    //    [self.navigationController presentViewController:vc animated:YES completion:nil];
    [self.navigationController pushViewController:vc animated:YES];
    
}

-(void)view_C4Clicked:(UITapGestureRecognizer *)tap
{
    SicknessPickViewController*vc=[[SicknessPickViewController alloc]init];
    vc.delegate=self;
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark - 获取疾病二级分类
-(void)loadDataWithCi1_id:(NSInteger)ci1_id
{
    
    if (ci1_id==6)
    {
        //跳转到公益活动网页
        return;
    }
    
    [[NetWorkManager sharedManager] loadDiseaseSubWithCi_id:(int)ci1_id page:15 successBlock:^(NSArray *dtoArray) {
        self.dataList=dtoArray.copy;
        
    } errorBlock:^(NSString *error) {
        NSLog(@"%@",error);
    }];
    
}
#pragma mark - 选择并发症列表接口
-(void)loadComplicationData{
    
    //    请求参数:{"page":1,"page_size":15,"ci2_id":3}
    NSString *urlString = kComplicationListUrl;
    NSDictionary *paramDic = @{@"page":@1,@"page_size":@15,@"ci2_id":@3
                               };
    
    //    [self loadUrl:urlString params:paramDic];
    [[NetWorkTool shareManager] POST_RequestWithUrlString:urlString parmas:paramDic successBloock:^(id responseObject) {
        NSArray*array=responseObject[@"data"];
        
        self.complicationDataList=[NSArray yy_modelArrayWithClass:[SicknessComplicationData class] json:array];
        
//        NSLog(@"responseObject = %@",responseObject);
    } failureBlock:^(NSError *error) {
        
        NSLog(@"error = %@",error);
    }];
    
}


#pragma mark - 根据条件加载符合的医生的数量
-(void)loadMatchedDoctor{
    
    /*
     接口注意： 这些数据都是死的
     
     */
    
    NSString *urlString = kMatchedDoctorCountUrl;
    NSDictionary *paramDic = @{@"ci1_id":@1,
                               @"ci2_id":@3,
                               @"ci3_id":@3,
                               @"diagnosis_type":@0,
                               @"is_confirmed":@1,
                               @"user_id":@1000089,
                               @"has_diagnosis":@2
                               };
    
    
    //    [self loadUrl:urlString params:paramDic];
    [[NetWorkTool shareManager] POST_RequestWithUrlString:urlString parmas:paramDic successBloock:^(id responseObject) {
        
     //   NSLog(@"responseObject = %@",responseObject);
        NSDictionary*dict=responseObject[@"data"];
        self.label_matchedDoctorCount.text=[NSString stringWithFormat:@"%@", dict[@"doctor_count"]];
        
    } failureBlock:^(NSError *error) {
        
        NSLog(@"error = %@",error);
    }];
    
    
}



#pragma mark - 是否确诊按钮的点击



/*
 [UIImage imageNamed:@"illness_rb_img_sel.png"]
 [UIImage imageNamed:@"illness_rb_img_nor.png"]
 UIControlStateNormal
 is_confirmed
 self.diagnosis_type=[[NSNumber alloc]initWithInt:0];
 */


- (IBAction)clickedButton_a1:(id)sender
{
    
    [self.button_a1 setImage:[UIImage imageNamed:@"illness_rb_img_sel.png"] forState:UIControlStateNormal];
    [self.button_a2 setImage: [UIImage imageNamed:@"illness_rb_img_nor.png"]
                    forState:UIControlStateNormal];
    self.is_confirmed=[[NSNumber alloc]initWithInt:1];
}
- (IBAction)clickedButton_a2:(id)sender {
    [self.button_a2 setImage:[UIImage imageNamed:@"illness_rb_img_sel.png"] forState:UIControlStateNormal];
    [self.button_a1 setImage: [UIImage imageNamed:@"illness_rb_img_nor.png"]
                    forState:UIControlStateNormal];
    self.is_confirmed=[[NSNumber alloc]initWithInt:0];

}

#pragma mark - 是否接受过治疗的按钮点击

/*
 has_diagnosis
 */
- (IBAction)clickedButton_b1:(id)sender
{
    [self.button_b1 setImage:[UIImage imageNamed:@"illness_rb_img_sel.png"] forState:UIControlStateNormal];
    [self.button_b2 setImage: [UIImage imageNamed:@"illness_rb_img_nor.png"]
                    forState:UIControlStateNormal];
    self.diagnosis_type=[[NSNumber alloc]initWithInt:1];
}
- (IBAction)clickedButton_b2:(id)sender
{
    [self.button_b2 setImage:[UIImage imageNamed:@"illness_rb_img_sel.png"] forState:UIControlStateNormal];
    [self.button_b1 setImage: [UIImage imageNamed:@"illness_rb_img_nor.png"]
                    forState:UIControlStateNormal];
    self.diagnosis_type=[[NSNumber alloc]initWithInt:0];
}


@end
